package cn.laike.modules.dev.controller;

import java.io.File;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import cn.laike.common.exception.RRException;
import cn.laike.common.utils.FileUtils;
import cn.laike.common.utils.PageUtils;
import cn.laike.common.utils.R;
import cn.laike.common.validator.ValidatorUtils;
import cn.laike.modules.dev.entity.HotelRoomBannerEntity;
import cn.laike.modules.dev.entity.HotelRoomEntity;
import cn.laike.modules.dev.service.HotelRoomBannerService;
import cn.laike.modules.dev.service.HotelRoomService;
import cn.laike.modules.dev.utils.DevUtils;
import cn.laike.modules.sys.shiro.ShiroUtils;



/**
 * 房型轮播图
 *
 * @author ziye
 * @email 524748907@qq.com
 * @date 2019-02-19 16:41:51
 */
@RestController
@RequestMapping("dev/hotelroombanner")
public class HotelRoomBannerController {
	@Autowired
    private HotelRoomService hotelRoomService;
	
    @Autowired
    private HotelRoomBannerService hotelRoomBannerService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("dev:hotelroombanner:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = hotelRoomBannerService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("dev:hotelroombanner:info")
    public R info(@PathVariable("id") Integer id){
        HotelRoomBannerEntity hotelRoomBanner = hotelRoomBannerService.selectById(id);

        return R.ok().put("hotelRoomBanner", hotelRoomBanner);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("dev:hotelroombanner:save")
    public R save(@RequestBody HotelRoomBannerEntity hotelRoomBanner){
    	if(hotelRoomBanner.getHotelId() == null || hotelRoomBanner.getHotelRoomId() == null) {
    		return R.error("参数异常！");
    	}
    	hotelRoomBanner.setCreateTime(new Date());
        hotelRoomBannerService.insert(hotelRoomBanner);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("dev:hotelroombanner:update")
    public R update(@RequestBody HotelRoomBannerEntity hotelRoomBanner){
        ValidatorUtils.validateEntity(hotelRoomBanner);
        hotelRoomBannerService.updateAllColumnById(hotelRoomBanner);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("dev:hotelroombanner:delete")
    public R delete(@RequestBody Integer[] ids){
        hotelRoomBannerService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

	@RequestMapping("/upload")
	public R upload(@RequestParam("picfile") MultipartFile picfile,HttpServletRequest request) throws Exception {
		if (picfile.isEmpty()) {
			throw new RRException("上传文件不能为空");
		}
		String url = "";
		if(picfile != null && picfile.getOriginalFilename().length()>4){
			String picFilePath = DevUtils.getHotelRoomBannerResourceSavePath()+File.separator;
			FileUtils.createDirectory(picFilePath);
			String sname = picfile.getOriginalFilename();
			String fileType = sname.substring(sname.lastIndexOf("."));
			String fname  = "banner"+ShiroUtils.getUserId() + "_"+System.currentTimeMillis()+fileType;
			File file = new File(picFilePath+fname);
			System.out.println("保存文件:"+picFilePath+fname);
			try {
				picfile.transferTo(file);
				url = file.getName();
			} catch (Exception e) {
				System.out.println("上传文件失败:"+picFilePath+fname);
				e.printStackTrace();
			}
		}
		return R.ok().put("url", url);
	}
}
